package com.yyg.usecase.spi.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public class ISayHelloService {

    public static Logger logger = LoggerFactory.getLogger(ISayHelloService.class);

    public static ISayHello iSayHello = null;

    public static List<ISayHello> iSayHelloList = new ArrayList<>();

    public static ISayHelloService iSayHelloService = new ISayHelloService();

    //构造函数
    public ISayHelloService(){
        ServiceLoader<ISayHello> serviceLoader = ServiceLoader.load(ISayHello.class);

        for (ISayHello iSayHello : serviceLoader) {
            iSayHelloList.add(iSayHello);
        }

        if (iSayHelloList.size() > 0) {
            iSayHello = iSayHelloList.get(0);
            logger.info("load ISayHello service success");
        } else {
            logger.warn("load ISayHello service fail");
        }
    }

    public static ISayHello getService() {
        return iSayHello;
    }

}
